{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 158,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import pyecharts as echart\n",
    "import numpy as np\n",
    "import os\n",
    "\n",
    "from typing import Dict\n",
    "from pyecharts.charts import Bar, Line, Tab\n",
    "from pyecharts.commons.utils import JsCode"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 159,
   "metadata": {},
   "outputs": [],
   "source": [
    "def read_excel_datas():\n",
    "    data_dir = \"./data\"\n",
    "    excel_sheet_name = [\n",
    "        \"总览表\", \"本科就业去向(单位性质)\", \n",
    "        \"硕士就业去向(单位性质)\", \"博士就业去向(单位性质)\",\n",
    "        \"本科升学去向\", \"硕士升学去向\", \"就业观念\"\n",
    "    ]\n",
    "\n",
    "    excel_file_names = os.listdir(data_dir)\n",
    "    excel_datas = dict()\n",
    "    for excel_file_name in excel_file_names:\n",
    "        excel_data = pd.read_excel(data_dir + \"/\" + excel_file_name, sheet_name=excel_sheet_name)\n",
    "        data_name = excel_file_name.split(\".\")[0]\n",
    "        excel_datas[data_name] = excel_data\n",
    "    \n",
    "    return excel_datas"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 160,
   "metadata": {},
   "outputs": [],
   "source": [
    "datas = read_excel_datas()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [],
   "source": [
    "def render_total(school: str, data: pd.DataFrame):\n",
    "    rate_college = \"本科升学率\"\n",
    "    years = [] \n",
    "    for year in data[\"年份\"].to_numpy():\n",
    "        years.append(str(year))\n",
    "    \n",
    "    (\n",
    "        Line()\n",
    "        .add_xaxis(years)\n",
    "        .add_yaxis(series_name=rate_college, y_axis=data[rate_college].to_numpy())\n",
    "        .set_global_opts(title_opts=echart.options.TitleOpts(title=school))\n",
    "        .render(school + \".html\")\n",
    "    )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 161,
   "metadata": {},
   "outputs": [],
   "source": [
    "def render_employment(school: str, level: str, data: pd.DataFrame) -> Line:\n",
    "    years = [] \n",
    "    for year in data[\"年份\"].to_numpy():\n",
    "        years.append(str(year))\n",
    "    \n",
    "    chart = (\n",
    "        Line()\n",
    "        .add_xaxis(years)\n",
    "        .set_global_opts(\n",
    "            title_opts=echart.options.TitleOpts(title=level + \"就业调查\"),\n",
    "            yaxis_opts=echart.options.AxisOpts(axislabel_opts=echart.options.LabelOpts(formatter=\"{value} %\"))\n",
    "        )\n",
    "    )\n",
    "\n",
    "    headers = [\"党政机关\", \"事业单位\", \"民营企业\", \"国有企业\", \"灵活就业\", \"创业\", \"其他\"]\n",
    "    for header in headers:\n",
    "        chart.add_yaxis(series_name=header, y_axis=data[header+\"%\"].to_numpy() * 100, \n",
    "        label_opts=echart.options.LabelOpts(formatter=JsCode(\"function (params) {return params.value[1].toFixed(1) + '%'}\")))\n",
    "    \n",
    "    return chart"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 162,
   "metadata": {},
   "outputs": [],
   "source": [
    "def render_upgrade(school: str, level: str, data: pd.DataFrame) -> Line:\n",
    "    years = [] \n",
    "    for year in data[\"年份\"].to_numpy():\n",
    "        years.append(str(year))\n",
    "    \n",
    "    chart = (\n",
    "        Line()\n",
    "        .add_xaxis(years)\n",
    "        .set_global_opts(\n",
    "            title_opts=echart.options.TitleOpts(title=level + \"升学去向调查\"),\n",
    "            yaxis_opts=echart.options.AxisOpts(axislabel_opts=echart.options.LabelOpts(formatter=\"{value} %\"))\n",
    "        )\n",
    "    )\n",
    "\n",
    "    headers = [\"中国大陆\", \"英国\", \"中国香港\"]\n",
    "    for header in headers:\n",
    "        chart.add_yaxis(series_name=header, y_axis=data[header+\"%\"].to_numpy() * 100, \n",
    "        label_opts=echart.options.LabelOpts(formatter=JsCode(\"function (params) {return params.value[1].toFixed(1) + '%'}\")))\n",
    "    \n",
    "    return chart"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 239,
   "metadata": {},
   "outputs": [],
   "source": [
    "def render_compare_employment(school: str, data: Dict[str, pd.DataFrame], entry: str) -> Bar:\n",
    "    years = [\"2018\", \"2019\", \"2020\", \"2021\"]\n",
    "    levels = [\"本科\", \"硕士\", \"博士\"]\n",
    "\n",
    "    chart = (\n",
    "        Bar()\n",
    "        .add_xaxis(years)\n",
    "        .set_global_opts(\n",
    "            title_opts=echart.options.TitleOpts(title=\"毕业生赴\" + entry + \"就业对比图\"),\n",
    "            yaxis_opts=echart.options.AxisOpts(axislabel_opts=echart.options.LabelOpts(formatter=\"{value} %\"))\n",
    "        )\n",
    "    )\n",
    "\n",
    "    for level in levels:\n",
    "        sheet_name = level + \"就业去向(单位性质)\";\n",
    "        # bug -> 这里没办法用 np.ndarray\n",
    "        list_data = []\n",
    "        for rate in data[sheet_name][entry+\"%\"].to_numpy() * 100:\n",
    "            list_data.append(round(rate, 1))\n",
    "        chart.add_yaxis(level, list_data)\n",
    "\n",
    "    return chart"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 244,
   "metadata": {},
   "outputs": [],
   "source": [
    "def render_compare_upgrade(school: str, data: Dict[str, pd.DataFrame], entry: str) -> Bar:\n",
    "    years = [\"2018\", \"2019\", \"2020\", \"2021\"]\n",
    "    levels = [\"本科\", \"硕士\"]\n",
    "\n",
    "    chart = (\n",
    "        Bar()\n",
    "        .add_xaxis(years)\n",
    "        .set_global_opts(\n",
    "            title_opts=echart.options.TitleOpts(title=\"毕业生赴\" + entry + \"深造对比图\"),\n",
    "            yaxis_opts=echart.options.AxisOpts(axislabel_opts=echart.options.LabelOpts(formatter=\"{value} %\"))\n",
    "        )\n",
    "    )\n",
    "\n",
    "    for level in levels:\n",
    "        sheet_name = level + \"升学去向\";\n",
    "        # bug -> 这里没办法用 np.ndarray\n",
    "        list_data = []\n",
    "        for rate in data[sheet_name][entry+\"%\"].to_numpy() * 100:\n",
    "            list_data.append(round(rate, 1))\n",
    "        chart.add_yaxis(level, list_data)\n",
    "\n",
    "    return chart"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 245,
   "metadata": {},
   "outputs": [],
   "source": [
    "def render_charts(excel_datas: Dict[str, pd.DataFrame]):\n",
    "    for (school, data) in excel_datas.items():\n",
    "        charts = Tab()\n",
    "\n",
    "        college_employment_line = render_employment(school, \"本科\", data[\"本科就业去向(单位性质)\"])\n",
    "        charts.add(college_employment_line, \"本科就业去向调查\")\n",
    "        master_employment_line = render_employment(school, \"硕士\", data[\"硕士就业去向(单位性质)\"])\n",
    "        charts.add(master_employment_line, \"硕士就业去向调查\")\n",
    "        doctor_employment_line = render_employment(school, \"博士\", data[\"博士就业去向(单位性质)\"])\n",
    "        charts.add(doctor_employment_line, \"博士就业去向调查\")\n",
    "\n",
    "        college_employment_line = render_upgrade(school, \"本科\", data[\"本科升学去向\"])\n",
    "        charts.add(college_employment_line, \"本科升学去向调查\")\n",
    "        master_employment_line = render_upgrade(school, \"硕士\", data[\"硕士升学去向\"])\n",
    "        charts.add(master_employment_line, \"硕士升学去向调查\")\n",
    "\n",
    "        headers = [\"党政机关\", \"事业单位\", \"民营企业\", \"国有企业\", \"灵活就业\", \"创业\", \"其他\"]\n",
    "\n",
    "        for header in headers:\n",
    "            compare = render_compare_employment(school, data, header)\n",
    "            charts.add(compare, \"毕业生赴\" + header + \"就业对比图\")\n",
    "\n",
    "        headers = [\"中国大陆\", \"英国\", \"中国香港\"]\n",
    "\n",
    "        for header in headers:\n",
    "            compare = render_compare_upgrade(school, data, header)\n",
    "            charts.add(compare, \"毕业生赴\" + header + \"深造对比图\")\n",
    "\n",
    "        charts.render(\"./charts/\" + school + \".html\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 246,
   "metadata": {},
   "outputs": [],
   "source": [
    "render_charts(datas)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.8.5 64-bit",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "b337b16e1f284c9fe7de692799556d56c1809887abe3f5a49ffeb9e7df151cfb"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
